home *** CD-ROM | disk | FTP | other *** search
- BASIC String Variables
- (BYTE Magazine June 1985 Review Feedback)
-
- In any BASIC program it is possible to substitute a variable for a
- string when it is not acceptable to the BASIC interpreters by leaving
- blank spaces within the quotation marks that need the string, then
- POKEing the string variable into the blanks character-by-character.
- This works in any variation of any BASIC.
- Most BASICs store keywords like OPEN, CLOSE, and GOTO as a 1-byte
- token instead of correctly spelling the keyword. This saves memory and
- speeds searches for the correct routine to execute the command. Some
- versions of Microsoft BASIC use 153 as the token for OPEN. The numbers
- are usually above the highest printing ASCII codes (above 127) and
- appear as graphics characters if you PEEK into the spot where the OPEN
- token is stored. If you loop through the computer's memory looking for
- the OPEN command's token, you will locate the line of code that needs to
- have the variable POKEd into the blanks between the quotation marks. In
- other words, you tell the computer to look through its memory starting
- at the beginning of your program and find the line you need by searching
- for OPEN.
- You need to look at a memory map of your computer to find out the
- address of the pointer that stores the address of the start of a BASIC
- program. The BST variable in line 200 of program 1 below is the address
- that points to the start of the BASIC program area, sometimes referred
- to as the BASIC buffer. BEND is the variable found by PEEKing the
- address of the start of the variables found in the pointer table. Some
- BASICs do not use a pointer to the end of your program; the end is
- simply defined by a series of zeros when the program is entered. You
- should find both BST and BEND before running the subroutine by PEEKing
- at the table of pointers and using the appropriate math. You can find
- them during program execution if you know the pointers for sure.
- If you do not have a memory map and do not know what the token for
- OPEN is, loop through the entire memory looking for a dummy line of
- text, then branch out of the loop and get the address where the dummy
- text was found (see program 2).
- Progarm 1 POKEs FL$ into the filename after OPEN in line 110. This
- whole section is treated as a subroutine and is exited after the CLOSE
- statement in line 130. More string manipulation could be done to check
- for a filename extension in FL$ instead of truncating FL$ when it is too
- long and forcing the extension to DAT. BST, BEND, and FL$ must be
- initialized before entering the subroutine.
- After you run this program, get a listing and see how line 110 is
- changed with FL$ in place of the blank spaces.
- Program 2 should be run independently of the first program and run
- several times using different characters in the remark statement in line
- 10, and in lines 50 and 110 to be certain there isn't a felonious group
- of Xs. After finding the start of BASIC, you find the end by adding the
- available memory in an empty buffer to the start address.
- - - - - -
- Program 1: POKEs FL$ into a filename
-
- 105 GOSUB 200
- 110 OPEN "0",#1," /DAT"
- 120 PRINT #1,"SOME DATA"
- 130 CLOSE #1
- 140 RETURN
- 200 FOR A=BST TO BEND
- 205 'BST & BEND are start and end of BASIC buffer
- 210 IF PEEK(A)=(TOKEN FOR "OPEN") THEN 250
- 215 'Line 210 branches at the OPEN token
- 220 NEXT A:RETURN
- 250 IF PEEK(A+1)=34 THEN 280
- 260 'Line 250 checks character after OPEN
- 265 'and branches at CHR$(34) or " sign
- 270 NEXT A:RETURN
- 280 IF PEEK(A+2)=ASC("0") THEN 310
- 290 'Line 280 is second redundancy check
- 300 NEXT A:RETURN
- 310 FL=8:IF LEN(FL$)<>8 THEN GOSUB 500
- 320 'Line 310 branches to padding routine to bring
- 325 'length of FL$ to 8 characters
- 330 C=0:FOR B=A+9 TO A+9+FL
- 340 C=C+1:POKE B,ASC(MID$(FL$,C,1))
- 350 NEXT B:NEXT A:RETURN
- 500 IF LEN(FL$)>8 THEN 550
- 510 FOR PAD=LEN(FL$) TO 7
- 520 FL$=FL$+" "
- 530 NEXT PAD:RETURN
- 550 FL$=LEFT$(FL$,8):RETURN
- - - - - -
- Program 2: Finds the start address of the BASIC buffer and the token for
- OPEN
- 10 'XXXXXXXXXXXXXXXX
- 20 OPEN "0",#1,"DUMMY"
- 30 CLOSE #1
- 40 FOR X=0 TO 65536
- 50 IF PEEK(X)=ASC("X") THEN 100
- 60 NEXT X:END
- 100 FOR Y=X TO X+16
- 110 IF PEK(Y)<>ASC("X") THEN 60
- 120 NEXT Y:PRINT "START ADDRESS OF BASIC IS";X-8
- 130 FOR Y=X TO X+25
- 140 IF PEEK(Y)=34 THEN 200
- 150 NEXT Y
- 200 PRINT"TOKEN FOR OPEN COMMAND IS";PEEK(Y-1)
-
- -----------------------------------------------------------------